/**
 * Created by Administrator on 2017/7/4 0004.
 */
$('#cmdb-groups-save-btn').delegate('.btn-primary','click',function () {
    var groupname = $('#cmdb-addgroup-input').val().trim();
    var groupdesc = $('#cmdb-addgroup-desc').val().trim();
    if(groupname.length==0){
        alert('组名不能为空')
    }else {
        $.ajax({
            url:'/cmdb/addhostgroup/',
            type:'POST',
            data:{'groupname':groupname,'groupdesc':groupdesc},
            success:function (data) {
                var retunData = JSON.parse(data);
                if(retunData.status == true){
                    var newTRtag=document.createElement('tr');
                    var td1='<td><a class="btn-link">'+ groupname  +'</a></td>';
                    var td2 = '<td>' + groupdesc + '</td>';
                    var td3 = '<td><span class="badge badge-primary">0</span></td>';
                    var td4 = '<td><button class="btn btn-danger">删除</button></td>'
                    $(newTRtag).append(td1);
                    $(newTRtag).append(td2);
                    $(newTRtag).append(td3);
                    $(newTRtag).append(td4);
                    $(newTRtag).attr('groupid',retunData.groupid);
                    $('#cmdb-group-databody').prepend(newTRtag);

                }
            }
        })
    }

$('#cmdb-addgroup-modal').modal('hide')
});
//del group
$('#t1').delegate('.cmdb-groups-delbtn','click',function () {
    var groupself = this;
    var groupid = $(groupself).parent().parent().attr('groupid');
    console.log(groupid)
    $.ajax({
        url:'/cmdb/delhostgroup/',
        type:'POST',
        data:{'groupid':groupid},
        success:function (data) {
            if(data=='ok'){
                console.log(data)
                $(groupself).parent().parent().remove() ;
            }
        }
    })
});



// 当模态框隐藏式，初始化
$('#cmdb-addgroup-modal').on('hidden.bs.modal', function () {
    $('#cmdb-addgroup-input').val('');
    $('#cmdb-addgroup-desc').val('');
});